var $btn;
$("#tbl_role").bootstrapTable({
    url:"role/list",
    method:'post',
    detailView:true,
    toolbar:'#toolbar',
    contentType:'application/x-www-form-urlencoded',
    sidePagination:'server',
    pageNumber:1,
    pageSize:5,
    queryParams:function(params){
        return params;
    },
    pageList:[5, 25, 50, 100,'ALL'],
    dataType:'json',
    cache:false,
    pagination:true,
    showRefresh:true,
    showColumns:true,
    classes:'table table-hover table-striped',
    columns:[
        {
            checkbox:true
        },
        {
            field:"id",
            title:"主键",
            align:"center"
        },{
            field:"name",
            title:"用户名",
            align:"center"
        },{
            title:"操作",
            align:"center",
            formatter:function (value, row, index) {
                var html = [
                    "<a href='javascript:void(0);' onclick='Add(\"修改权限\",\"privileges/update?id="+row.id+"\")'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;",
                    "<a href='javascript:void(0);' onclick='delRow2("+row.id+")'><span class='glyphicon glyphicon-remove'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;",
                    // "<button onclick='changePrivileges("+row.id+",this)' class='disabled btn btn-xs btn-danger'>修改权限</button>"
                ];
                return html.join("");
            }
        }
    ],
    onExpandRow:function(index, row, $Subdetail){
        InitSubTable(index, row, $Subdetail);
    }

});

    function toAdd(title,url) {
        $("#modal1").find(".modal-title").html(title);
        $("#modal1").find(".modal-body").load(url);
        $("#modal1").modal('show');
    }
    function Add(title,url) {
        $("#modal2").find(".modal-title").html(title);
        $("#modal2").find(".modal-body").load(url);
        $("#modal2").modal('show');
    }


    $('.dropdown-mul-1').dropdown({
        limitCount: 40,
        multipleMode: 'label',
        choice: function () {
            console.log(arguments,this);
        }
    });

InitSubTable = function (index, row, $detail) {
    var parentid = row.id;
    var cur_table = $detail.html('<table></table>').find('table');
    $(cur_table).bootstrapTable({
        url: 'privileges/getById',
        method: 'post',
        toolbar:'#toolbar',
        contentType:'application/x-www-form-urlencoded',
        sidePagination:'server',
        dataType:'json',
        queryParams: { id: parentid },
        ajaxOptions: { id: parentid },
        clickToSelect: true,
        pageSize: 10,
        pageList: [10, 25],
        columns: [{
                visible: false,
                field: "id",
                title: "德玛",
                align: "center"
            }, {
            title:"序号",
            align:"center",
            formatter:function(value,row,index){
                return (index + 1);
            }
        }, {
            field: 'privilege.name',
            title: '权限姓名'
        },{
            title:"操作",
            align:"center",
            formatter:function (value, row, index) {
                var html = [
                "<a href='javascript:void(0);' onclick='delRow("+row.id+")'><span class='glyphicon glyphicon-remove'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;",
                ];
                return html.join("");
            }
        } ],
        //无线循环取子表，直到子表里面没有记录
        onExpandRow: function (index, row, $Subdetail) {
            oInit.InitSubTable(index, row, $Subdetail);
        }
    });
};

function delRow(id) {
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        $.ajax({
            url:'privileges/delete',
            method:"post",
            data:{"id":id},
            success:function (data) {
                swal("删除成功！", "您已经永久删除了这条信息。", "success");
                //刷新表格
                $("#tbl_role").bootstrapTable('refresh');
            }
        });


    });
}

function delRow2(id) {
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        $.ajax({
            url:'role/delete',
            method:"post",
            data:{"id":id},
            success:function (data) {
                swal("删除成功！", "您已经永久删除了这条信息。", "success");
                //刷新表格
                $("#tbl_role").bootstrapTable('refresh');
            }
        });


    });
}

function submitForm(i) {
    var $form = $(i).parents("#modal2").find("#myForm");
    var action = $form.attr("action");
    var a=[];
    var b=0;
    var shuzu = $form.children("input[name='sel']").last().index();
    var id=$form.children("#id").val();
    for (var i=0;i<shuzu;i++){
        if($form.children("input[name='sel']").eq(i).prop("checked")){
            a[b] = $form.children("input[name='sel']").eq(i).val();
            b++;
        }

    }
    $.ajax({
        url:action,
        method:"post",
        //data:str,
        //FormData(dom)
        data:{"shuzu":a,"id":id},
        dataType:'json',
        // processData: false,
        // contentType: false,
        traditional: true,
        success:function (data) {
            if(data.result){
                swal("成功", "操作成功", "success");
                //隐藏模态框
                $("#modal2").modal('hide');
                //刷新表格
                $("#tbl_role").bootstrapTable('refresh');

            }
        }
    });
}

function submitf(i) {

    var $form = $(i).parents("#modal1").find("#myForm1");
    var action = $form.attr("action");
    var str = $form.serialize();
    $.ajax({
        url:action,
        method:"post",
        //data:str,
        //FormData(dom)
        data:str,
        dataType:'json',
        // processData: false,
        // contentType: false,
        traditional: true,
        success:function (data) {
            if(data.result){
                swal("成功", "操作成功", "success");
                //隐藏模态框
                $("#modal1").modal('hide');
                //刷新表格
                $("#tbl_role").bootstrapTable('refresh');

            }
        }
    });
}

function changePrivileges(roleid,btn) {
    var data = {"roleid":roleid,"privList":[]};
    var $detail = $(btn).parents('tr').next();
    var checkboxs = $detail.find('input[type=checkbox]:checked');
    for(var i = 0;i<checkboxs.length;i++){
        data.privList.push($(checkboxs[i]).val());
    }
    $.post('role/changePrivileges',{"str":JSON.stringify(data)},function (result) {
        if(result.result){
            swal("删除成功!","完成!","success");
        }
    },'json');
}